package cc.customer.cusDynamic.services.impl;

import cc.customer.common.domain.MRecord;
import cc.customer.expCustomer.dao.ExEmpMapper;
import cc.customer.expCustomer.domain.ExEmp;
import cc.customer.aiEmport.damain.CAiEmp;
import cc.customer.aiEmport.dao.CaiEmpMapper;
import cc.customer.bigEmport.damain.BigEmp;
import cc.customer.bigEmport.dao.BigEmpMapper;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.cusDynamic.dao.CusDynamicMapper;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.domain.ExEmpRecevice;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.dao.MyCustomerMapper;
import cc.customer.onschool.dao.OnSchoolResourceMapper;
import cc.customer.onschool.domain.OnSchoolResource;
import cc.customer.MRecord.dao.MRecordMapper;
import cc.customer.system.dao.SaleTagMapper;
import cc.customer.system.dao.UserMapper;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.utils.AutonCusInfo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class CusDynamicImpl extends BaseService<CusDynamic> implements CusDynamicService {
   private static final Logger logger = LoggerFactory.getLogger(CusDynamicImpl.class);
   @Autowired
   private CusDynamicMapper cusDynamicMapper;
   @Autowired
   private UserService userService;
   @Autowired
   private SaleTagMapper saleTagDao;
   @Autowired
   private ExEmpMapper exEmpMapper;
   @Autowired
   private BigEmpMapper bigEmpMapper;
   @Autowired
   private CaiEmpMapper caiEmpMapper;

   @Autowired
   private OnSchoolResourceMapper onSchoolResourceMapper;
   @Autowired
   private MyCustomerMapper myCustomerMapper;
   @Autowired
   private UserMapper userMapper;
   @Autowired
   private MRecordMapper recordMapper;

   @Override
   public synchronized void inserDynamic(List<String> list, CusDynamic dynamic){
      dynamic.setDyUuid(Utils.getUUID());
      if(Constant.CUSTOMER_RECORD_GIVEUP.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("放弃客户");
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_LINGQU.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_LINGQU);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("领取客户");
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_SLAPLAN.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_SLAPLAN);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("添加销售计划");   //没有作用
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_ZHUANRQANG.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("转让客户");
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_ZGENGXIN.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_ZGENGXIN);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("更新客户资料");  //没有作用
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_GONGHAIFENPEI.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("公海客户重新分配");
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_SHARCUSTOMER.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
         for(String id:list) {
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("共享客户");
            cusDynamicMapper.insert(dynamic);
         }
      }else if(Constant.CUSTOMER_RECORD_UPDATECUSTONER.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
         for(String id:list){
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("更新客户标签");
            cusDynamicMapper.insert(dynamic);
         }

      } else if(Constant.CUSTOMER_RECORD_DIAPHONE.equals(dynamic.getDyType())){
         dynamic.setDyType(Constant.CUSTOMER_RECORD_DIAPHONE);
         for(String id:list){
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("拨打电话");
            cusDynamicMapper.insert(dynamic);
         }
      }else if (Constant.CUSTOMER_RECORD_GONGHAILINGQU.equals(dynamic.getDyType())){
         for(String id:list){
            dynamic.setDyCusUuid(id);
            dynamic.setDyContent("公海领取客户");
            cusDynamicMapper.insert(dynamic);
         }
      }else if (Constant.CUSTOMER_RECORD_STATAGES.equals(dynamic.getDyType())){
         for(String id:list){
            dynamic.setDyCusUuid(id);
            cusDynamicMapper.insert(dynamic);
         }
      }
   }
   /**大资源批量插入**/
   @Override
   @Transactional
   public int batchInsertBigEmpDisture(List<String> ids, BigEmp bigEmp) throws Exception {
      try {
         String person = bigEmp.getBpDisPerson();
         List<CusDynamic> list = new ArrayList<>();
         for (String id :ids){
            CusDynamic cusDynamic = new CusDynamic();
            cusDynamic.setDyType("13");
            cusDynamic.setDyUuid(Utils.getUUID());
            cusDynamic.setDySourceType("3");
            cusDynamic.setDyCusUuid(id);
            cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            cusDynamic.setDyCreator(person);
            cusDynamic.setDyContent(bigEmp.getBpRecent());
            cusDynamic.setDyDisUserid(bigEmp.getBpDistribute());
            list.add(cusDynamic);
         }
         return cusDynamicMapper.insertList(list);
      }catch (Exception e){
         throw  new RuntimeException();
      }
   }
   /**Ai资源批量分配记录**/
   @Override
   @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
   public int batchInsertAiEmpDisture(List<String> ids, CAiEmp cAiEmp) throws Exception {
      try {
         String person = cAiEmp.getAiDisPerson();
         List<CusDynamic> list = new ArrayList<>();
         for (String id :ids){
            CusDynamic cusDynamic = new CusDynamic();
            cusDynamic.setDyType("20");
            cusDynamic.setDyUuid(Utils.getUUID());
            cusDynamic.setDySourceType("5");
            cusDynamic.setDyCusUuid(id);
            cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            cusDynamic.setDyCreator(person);
            cusDynamic.setDyContent(cAiEmp.getAiRecent());
            cusDynamic.setDyDisUserid(cAiEmp.getAiDistriute());
            list.add(cusDynamic);
         }
         return cusDynamicMapper.insertList(list);
      }catch (Exception e){
         e.printStackTrace();
         throw  new RuntimeException();
      }
   }
   /**网校资源分配记录**/
   @Override
   @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
   public int batchInsertWxEmpDisture(List<String> ids, OnSchoolResource schoolResource) throws Exception {
      try {
         String person =schoolResource.getOsDistributor();  //执行分配的人
         List<CusDynamic> list = new ArrayList<>();
         for (String id :ids){
            CusDynamic cusDynamic = new CusDynamic();
            cusDynamic.setDyType("21");
            cusDynamic.setDyUuid(Utils.getUUID());
            cusDynamic.setDySourceType("2");
            cusDynamic.setDyCusUuid(id);
            cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            cusDynamic.setDyCreator(person);
            cusDynamic.setDyContent(schoolResource.getOsRecent());
            cusDynamic.setDyDisUserid(schoolResource.getOsMyCustomer());
            list.add(cusDynamic);
         }
         return cusDynamicMapper.insertList(list);
      }catch (Exception e){
         e.printStackTrace();
         throw  new RuntimeException();
      }
   }

   /**app添加客户跟进*/
   @Override
   public ResponseBo addAppFollow(CusDynamic cusDynamic, String userid){
      cusDynamic.setDyCreator(userid);
      cusDynamic.setDyType("9");
      cusDynamic.setDyContent("添加跟进记录");
      cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      cusDynamic.setDyUuid(Utils.getUUID());
      try{
         //资源类型
         String type = cusDynamic.getDySourceType();
         String uuid = cusDynamic.getDyCusUuid();
         String cusname = "";
         if(type.equals(Constant.ZIYUAN_TYPE_EXEMP)){
            Example example = new Example(ExEmp.class);
            example.createCriteria().andEqualTo("cExId",uuid);
            cusname = exEmpMapper.selectByExample(example).get(0).getcExName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_SCHOLLE)){
            Example example = new Example(OnSchoolResource.class);
            example.createCriteria().andEqualTo("osId",uuid);
            cusname = onSchoolResourceMapper.selectByExample(example).get(0).getOsNickname();

         }else if(type.equals(Constant.ZIYUAN_TYPE_BIGENP)){
            Example example = new Example(BigEmp.class);
            example.createCriteria().andEqualTo("bpId",uuid);
            cusname = bigEmpMapper.selectByExample(example).get(0).getBpName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_MINE)){
            Example example = new Example(MyCustomer.class);
            example.createCriteria().andEqualTo("myCusId",uuid);
            cusname = myCustomerMapper.selectByExample(example).get(0).getMyCusName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_AI)){
            Example example = new Example(CAiEmp.class);
            example.createCriteria().andEqualTo("aiId",uuid);
            cusname = caiEmpMapper.selectByExample(example).get(0).getAiName();
         }
         cusDynamic.setDyCusName(cusname);

         cusDynamicMapper.insert(cusDynamic);
         return ResponseBo.ok("添加成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("添加失败，请联系网站管理员");
      }
   }


   /**添加客户跟进*/
   @Override
   public ResponseBo addFollow(CusDynamic cusDynamic, String userid){
      cusDynamic.setDyCreator(userid);
      cusDynamic.setDyType("9");
      cusDynamic.setDyContent("添加跟进记录");
      cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      cusDynamic.setDyUuid(Utils.getUUID());
      try{
         //资源类型
         String type = cusDynamic.getDySourceType();
         String uuid = cusDynamic.getDyCusUuid();
         String cusname = "";
         if(type.equals(Constant.ZIYUAN_TYPE_EXEMP)){
            Example example = new Example(ExEmp.class);
            example.createCriteria().andEqualTo("cExId",uuid);
            cusname = exEmpMapper.selectByExample(example).get(0).getcExName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_SCHOLLE)){
            Example example = new Example(OnSchoolResource.class);
            example.createCriteria().andEqualTo("osId",uuid);
            cusname = onSchoolResourceMapper.selectByExample(example).get(0).getOsNickname();

         }else if(type.equals(Constant.ZIYUAN_TYPE_BIGENP)){
            Example example = new Example(BigEmp.class);
            example.createCriteria().andEqualTo("bpId",uuid);
            cusname = bigEmpMapper.selectByExample(example).get(0).getBpName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_MINE)){
            Example example = new Example(MyCustomer.class);
            example.createCriteria().andEqualTo("myCusId",uuid);
            cusname = myCustomerMapper.selectByExample(example).get(0).getMyCusName();

         }else if(type.equals(Constant.ZIYUAN_TYPE_AI)){
            Example example = new Example(CAiEmp.class);
            example.createCriteria().andEqualTo("aiId",uuid);
            cusname = caiEmpMapper.selectByExample(example).get(0).getAiName();
         }
         cusDynamic.setDyCusName(cusname);

         cusDynamicMapper.insert(cusDynamic);
         return ResponseBo.ok("添加成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("添加失败，请联系网站管理员");
      }
   }
   /**手动分配推广资源用户***/
   @Override
   public int insertExEmpDis(String userid,String uid) throws Exception {
      CusDynamic cusDynamic = new CusDynamic();
      cusDynamic.setDyCreator(userid);
      cusDynamic.setDyType("14");
      cusDynamic.setDyContent("手动分配推广资源");
      cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      cusDynamic.setDyUuid(Utils.getUUID());
      cusDynamic.setDySourceType("1");
      cusDynamic.setDyCusUuid(uid);
      try{
        return cusDynamicMapper.insert(cusDynamic);
      }catch (Exception e){
        throw new RuntimeException();
      }
   }

   /**查找客户跟进详情*/
   @Override
   public ResponseBo appGetDetailFollow(CusDynamic cus, String userid){
      try {
         List<CusDynamic> list = cusDynamicMapper.selectDetailFollow(cus);
         String content = "";
         String tname = "";
         for(CusDynamic dyc:list){
            String type = dyc.getDyType();
            if(Constant.CUSTOMER_RECORD_ADD.equals(type)){
               tname = "客户新增";
               content = "新增客户" ;
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_GIVEUP.equals(type)){
               tname = "放弃客户";
               content = "放弃了客户" ;
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_LINGQU.equals(type)){
               tname = "领取客户";
               content = "领取了客户";
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_SLAPLAN.equals(type)){
               tname = "添加销售计划";
               content = "添加销售计划";
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_ZHUANRQANG.equals(type)){
               tname = "转让客户";
               content = "转让了客户，转让给："+dyc.getDyZhuanName();
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_GONGHAIFENPEI.equals(type)){
               tname = "客户重新分配";
               content = "公司大公海客户重新分配，分配给："+dyc.getDyDisName();
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_SHARCUSTOMER.equals(type)){
               String[] sharids = dyc.getDyShareUserid().split(",");
               StringBuffer sb = new StringBuffer();
               for(String id : sharids){
                  String username = userService.findUserByUuids(id).getNickname();
                  sb.append(username+",");
               }
               String names = sb.substring(0,sb.lastIndexOf(","));
               tname = "共享客户";
               content = "共享了客户，共享关系人："+names;
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_GENJINRECOD.equals(type)){
               tname = "添加跟进记录";
               content = dyc.getDyFollowContent();
               dyc.setDyFollowContent(content);
            }else if(Constant.CUSTOMER_RECORD_MOBILE.equals(type)) {


            } else if(Constant.CUSTOMER_RECORD_DIAPHONE.equals(type)){
               tname = "拨打了电话";
               String url= dyc.getDyUrl();
               if (!StringUtils.isEmpty(url)){
                  url = Constant.BIGEMP_FILE_RECORD_PATH+JSONObject.parseObject(url).getString("url");
                  content = url;
               }else{
                  content = "";
               }
               dyc.setDyMPath(content);
               dyc.setDyFollowContent("拨打了电话");
            }else  if(Constant.CDY_LOGS_CUSSYSUP.equals(type)){
               tname = "系统放弃";
               content = "系统认为客户不可跟踪自动放弃";
               dyc.setDyFollowContent(content);
            }else  if(Constant.CDY_LOGS_CUSSREOUSER.equals(type)){
               tname = "大资源分配";
               content = dyc.getDyContent();
               dyc.setDyFollowContent(content);
            }else  if(Constant.CDY_LOGS_CUSSHOUFEBNPEI.equals(type)){
               tname = "推广资源手动分配";
               content = dyc.getDyContent();
               dyc.setDyFollowContent(content);
            }else  if(Constant.CUSTOMER_RECORD_GONGHAILINGQU.equals(type)){
               tname = "公司公海领取";
               content = dyc.getDyContent();
               dyc.setDyFollowContent(content);
            } else  if(Constant.CUSTOMER_RECORD_STATAGES.equals(type)){
               tname = "客户阶段";
               content = dyc.getDyContent();
               dyc.setDyFollowContent(content);
            }
         }
         return ResponseBo.ok("用户跟进列表查询成功",list);
      } catch (Exception e) {
         e.printStackTrace();
         return ResponseBo.error("用户跟进列表查询失败");
      }
   }

   /**查找客户跟进详情*/
   @Override
   public ResponseBo getDetailFollow(CusDynamic cus, String userid){
      try{
         List<CusDynamic> list = cusDynamicMapper.selectDetailFollow(cus);
         StringBuffer finalcontent = new StringBuffer();
         if(list.size()>0){
            String pic = "<div class='col-md-12 col-sm-12 col-xs-12 p-l-0 m-t-20'>" +
                    "<div class='pull-left col-md-1 col-sm-1 col-xs-6 p-l-0 p-r-0'>" +
                    " <img src='img/avatar/customer.png' class='width-30 height-30' /></div>" +
                    "<div class='pull-right p-l-0 p-r-0 col-md-11 col-sm-11 col-xs-12' style='min-height:85px;max-height:300px;border:solid 0.5px #d9d9d9;box-shadow:3px 3px 10px #888888;border-radius:4px;'><div>";
            String end = "</div></div></div>";
            String content = "";
            String tname = "";
            for(CusDynamic dyc:list){
               String type = dyc.getDyType();
               if(Constant.CUSTOMER_RECORD_ADD.equals(type)){
                  tname = "客户新增";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                            "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                            "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>新增客户<p></div>" +
                            "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_GIVEUP.equals(type)){
                  tname = "放弃客户";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>放弃了客户<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_LINGQU.equals(type)){
                  tname = "领取客户";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>领取了客户<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_SLAPLAN.equals(type)){
                  tname = "添加销售计划";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>添加销售计划<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_ZHUANRQANG.equals(type)){
                  tname = "转让客户";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>转让了客户，转让给：<strong class='color-blue'>"+dyc.getDyZhuanName()+"</strong><p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_GONGHAIFENPEI.equals(type)){
                  tname = "客户重新分配";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>公司大公海客户重新分配，分配给：<strong class='color-blue'>"+dyc.getDyDisName()+"</strong><p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_SHARCUSTOMER.equals(type)){
                  String[] sharids = dyc.getDyShareUserid().split(",");
                  StringBuffer sb = new StringBuffer();
                  for(String id : sharids){
                     String username = userService.findUserByUuids(id).getNickname();
                     sb.append(username+",");
                  }
                  String names = sb.substring(0,sb.lastIndexOf(","));
                  tname = "共享客户";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>共享了客户，共享关系人：<strong class='color-blue'>"+names+"</strong><p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else if(Constant.CUSTOMER_RECORD_GENJINRECOD.equals(type)){
                  tname = "添加跟进记录";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyFollowContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);

               }else if(Constant.CUSTOMER_RECORD_MOBILE.equals(type)){
                  tname = "手机拨打电话";
                  String url= dyc.getDyMPath();
                  if (!"isnull".equals(url)){

                     content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                             "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                             "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>" +
                             "<audio class='height-30' src="+url+"  controls=\"controls\" type=\"audio/wav\"></audio><p></div>" +
                           //  "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyFollowContent()+"<p></div>" +
                             "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  }else{
                     content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                             "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                             "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10 text-danger'>" +
                             dyc.getDyFollowContent()+"<p></div>" +
                             "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  }
                 /* content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyFollowContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";*/
                  finalcontent.append(pic + content + end);
               }/*else if(Constant.CUSTOMER_RECORD_UPDATECUSTONER.equals(type)){
                  String[] labelid = dyc.getDyLabelAfter().split(",");
                  StringBuffer sb = new StringBuffer();
//                  for(String label : labelid){
//                     String labelname = saleTagDao.selectTagNameById(label);
//                     sb.append(labelname+",");
//                  }
                  StringBuffer sbaler= new StringBuffer();
                  String beforeLabel = dyc.getDyLabelBefore();
                  if (!StringUtils.isEmpty(beforeLabel)){
                     String []belael = beforeLabel.split(",");
//                     for(String label : belael){
//                        String labelname = saleTagDao.selectTagNameById(label);
//                        sbaler.append(labelname+",");
//                     }
                  }else{ sbaler.append("空"); }
                  String labName = sb.substring(0,sb.lastIndexOf(","));
                  tname = "更新客户标签";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>客户标签:<strong class='color-blue'>"+sbaler+"</strong>变更为<strong class='color-blue'>"+labName+"</strong><p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }*/ else if(Constant.CUSTOMER_RECORD_DIAPHONE.equals(type)){
                  tname = "拨打了电话";
                  String url= dyc.getDyUrl();
                  if (!StringUtils.isEmpty(url)){
                     url = Constant.BIGEMP_FILE_RECORD_PATH+JSONObject.parseObject(url).getString("url");
                     content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                             "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                             "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>" +
                             "<audio class='height-30' src="+url+"  controls=\"controls\" type=\"audio/wav\"></audio><p></div>" +
                             "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  }else{
                     content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>"+dyc.getDyCreatorName()+"</p></div>" +
                             "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                             "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10 text-danger'>" +
                             "解析录音失败<p></div>" +
                             "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  }
                  finalcontent.append(pic + content + end);
               }else  if(Constant.CDY_LOGS_CUSSYSUP.equals(type)){
                  tname = "系统放弃";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>跟进人: "+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>系统认为客户不可跟踪自动放弃<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else  if(Constant.CDY_LOGS_CUSSREOUSER.equals(type)){
                  tname = "大资源分配";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>跟进人: "+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else  if(Constant.CDY_LOGS_CUSSHOUFEBNPEI.equals(type)){
                  tname = "推广资源手动分配";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>跟进人: "+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }else  if(Constant.CUSTOMER_RECORD_GONGHAILINGQU.equals(type)){
                  tname = "公司公海领取";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>操作人: "+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               } else  if(Constant.CUSTOMER_RECORD_STATAGES.equals(type)){
                  tname = "客户阶段";
                  content = "<div class='pull-left'><p class='p-l-10 p-t-3 m-5 color-blue'>操作人: "+dyc.getDyCreatorName()+"</p></div>" +
                          "<div class='pull-right'><p class='p-t-5 p-r-10 m-5'>"+tname+"</p></div><br/>" +
                          "<div class='col-md-12 col-sm-12 col-xs-12'><p class='pull-left m-b-10'>"+dyc.getDyContent()+"<p></div>" +
                          "<div class='pull-left'><p class='p-l-15'>"+dyc.getDyCreattime()+"</p></div>";
                  finalcontent.append(pic + content + end);
               }
            }
         }
         return ResponseBo.ok("",finalcontent.toString());
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }

   /**查看列表**/
   @Override
   public ResponseBo getFollowList(CusDynamic cusDynamic) {
      try {
         JSONObject jsonObject = new JSONObject();

         /**时间格式化**/
         String starttime = cusDynamic.getStarttime();
         if("7".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-7);
            String st = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            String et = DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59";
            cusDynamic.setStarttime(st); cusDynamic.setEndtime(et);
         }else if ("30".equals(starttime)){
            Date month = DateUtil.addDays(new Date(),-30);
            String st = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
            String et = DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59";
            cusDynamic.setStarttime(st); cusDynamic.setEndtime(et);
         }else{
            String endtime = cusDynamic.getEndtime();
            if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
               String st =starttime+" 00:00:00";
               String et = endtime+" 23:59:59";
               cusDynamic.setStarttime(st); cusDynamic.setEndtime(et);
            }
         }
         /**标签选择**/
         String lanelid = cusDynamic.getDyLabelAfter();
         String label = cusDynamic.getDyLabelBefore();
         //表头
         List<String> labelName = new ArrayList<>();
         labelName.add("名称");
         labelName.addAll(Arrays.asList(label.split(",")));
         jsonObject.put("head",labelName); //添加表头

         List<String> lanelids = Arrays.asList(lanelid.split(","));

         /**人员选择**/
         String userid = cusDynamic.getDyCusUuid();
         String name = cusDynamic.getDyCreatorName();
         JSONArray jsonArray = new JSONArray();
         if (StringUtils.isEmpty(userid)){
            //查询所有人
            Example example = new Example(MyUser.class);
            example.createCriteria().andEqualTo("status","1");
            List<MyUser> listall =  userService.selectByExample(example);
            for (MyUser user:listall){
               JSONArray jsonObjectall = new JSONArray();
               jsonObjectall.add(user.getNickname());
               /**获取标签**/
               for (String labelids :lanelids){
                  Example example1 = new Example(CusDynamic.class);
                  example1.createCriteria().andEqualTo("dyCreator",user.getUserUuid()).andLike("dyLabelAfter","%"+labelids+"%")
                          .andBetween("dyCreattime",cusDynamic.getStarttime(),cusDynamic.getEndtime()).andEqualTo("dyType","10");
                  int num = cusDynamicMapper.selectCountByExample(example1);
                  jsonObjectall.add(num);
               }
               jsonArray.add(jsonObjectall);
            }

         }else{
            //不为空
            List<String> listUuids = Arrays.asList(userid.split(","));
            List<String> listName = Arrays.asList(name.split(","));
            for (int i=0;i<listUuids.size();i++){
               String uuids = listUuids.get(i);
               List<String> users = new ArrayList<>();
               if (uuids.contains("jpet")){
                  String deptid = uuids.replace("jpet","");
                  Example example = new Example(MyUser.class);
                  example.createCriteria().andEqualTo("deptId",deptid).andEqualTo("status","1");
                  List<MyUser> listall =  userService.selectByExample(example);
                  for (MyUser user:listall){
                     users.add(user.getUserUuid());
                  }
               }else{
                  users.add(uuids);
               }
               JSONArray jsonObjectall = new JSONArray();
               jsonObjectall.add(listName.get(i));
               /**获取标签**/
               for (String labelids :lanelids){
                  Example example = new Example(CusDynamic.class);
                  example.createCriteria().andIn("dyCreator",users).andLike("dyLabelAfter","%"+labelids+"%")
                          .andBetween("dyCreattime",cusDynamic.getStarttime(),cusDynamic.getEndtime()).andEqualTo("dyType","10");;
                  int num = cusDynamicMapper.selectCountByExample(example);
                  jsonObjectall.add(num);
               }
               jsonArray.add(jsonObjectall);
            }
         }
         jsonObject.put("column",jsonArray);
         return ResponseBo.ok(jsonObject);
      }catch (Exception e){
         e.printStackTrace();
      }
      return ResponseBo.error();
   }

   /**根据当前登录人加载跟进记录*/
   @Override
   public List<CusDynamic> selectCusFollow(CusDynamic cusDynamic){
      try{
         /**时间格式化选择**/
         String starttime = cusDynamic.getStarttime();
         if("1".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-0);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else if("2".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-1);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.convert2String(week,"yyyy-MM-dd")+" 23:59:59");
         }else if("7".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-7);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else if ("30".equals(starttime)){
            Date month = DateUtil.addDays(new Date(),-30);
            String datestr = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else{
            /**时间格式化**/
            String endtime = cusDynamic.getEndtime();
            if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
               String timestart = starttime+" 00:00:00";
               String timeend = endtime+" 23:59:59";
               cusDynamic.setStarttime(timestart); cusDynamic.setEndtime(timeend);
            }
         }
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         String endtime = cusDynamic.getEndtime();
         String stattime = cusDynamic.getStarttime();

         if (!StringUtils.isAnyEmpty(stattime,endtime)){
            criteria.andBetween("dyCreattime",stattime,endtime+" 23:59:59");
         }

         /**人员查询**/
         List<String> selectUser = cusDynamic.getCreatorList();
         if (!selectUser.isEmpty()){
            criteria.andIn("dyCreator",selectUser);
         }

         /**查询客户名称*/
         if(!StringUtils.isEmpty(cusDynamic.getDyCusName())){
            criteria.andLike("dyCusName","%"+cusDynamic.getDyCusName()+"%");
         }

         /**推广资源条件**/
         criteria.andEqualTo("dyType","9");

         /**排序**/
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> list = this.selectByExample(example);
         for (CusDynamic cus:list){
            MyUser myUsers = userService.findUserByUuids(cus.getDyCreator());
            if (myUsers !=null) cus.setDyCreatorName(myUsers.getNickname());
            String type = cus.getDySourceType();
            switch (type){
               case Constant.ZIYUAN_TYPE_EXEMP:
                  ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
                  if(exEmp!=null){cus.setDyDisName(exEmp.getcExName());}

                  break;
               case Constant.ZIYUAN_TYPE_BIGENP:
                  BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  if(bigEmp!=null){
                     cus.setDyDisName(bigEmp.getBpName());
                  }
                  break;
               case Constant.ZIYUAN_TYPE_SCHOLLE:
                  OnSchoolResource onSchoolRes = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  if(onSchoolRes!=null){cus.setDyDisName(onSchoolRes.getOsNickname());}
                  break;
               case Constant.ZIYUAN_TYPE_MINE:
                  MyCustomer myCustomer = myCustomerMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  if(myCustomer!=null){cus.setDyDisName(myCustomer.getMyCusName());}
                  break;
               case Constant.ZIYUAN_TYPE_AI:
                  CAiEmp aiEmp = caiEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  if(aiEmp!=null){cus.setDyDisName(aiEmp.getAiName());}
                  break;
                  default:
                     cus.setDyDisName("-");
            }
         }
         return list;
      }catch (Exception e){
         logger.error("获取跟进记录失败："+e.getMessage());
         e.printStackTrace();
         return new ArrayList<>();
      }
   }
   /**查个人沟通记录当天的所有内容*/
   @Override
   public ResponseBo selectAllRecordData(MyUser user){
      try{
         String useruuid = user.getUserUuid();
         String myusername = user.getNickname(); String today = DateUtil.getCurrentDate("yyyy-MM-dd");
         List<CusDynamic> list = cusDynamicMapper.selectAllRecordData(useruuid,today);
         if(list.size()>0){
            for(CusDynamic cus:list){
               String content = "";
               if("9".equals(cus.getDyType())){
                  String followCont = cus.getDyFollowContent();
                  String cont = "";
                  if(followCont.length()>50){
                     cont = followCont.substring(0,50)+"...";
                  }else{
                     cont = followCont;
                  }
                  content = "<div class=\"p-l-0 col-md-12 col-sm-12 col-xs-12 p-r-5\" style=\"margin-bottom:8px;background-color:#FAFAFA;height:55px;border:solid 0.5px #d9d9d9;border-radius:4px;\">\n" +
                          "     <div class=\"pull-left p-l-5 p-t-5\">\n" + "" +
                          "        <i class=\"fa fa-3x fa-file-text pull-left\" style=\"color: deepskyblue;\"></i>\n" + "" +
                          "        <p class=\"pull-left\" style=\"color: black;\">添加跟进记录给 ：</p>\n" + "" +
                          "        <p class=\"pull-right color-blue\"><a href='javascript:;' cusid='"+cus.getDyCusUuid()+"' typeid='"+cus.getDySourceType()+"' onclick='selectCusDetail(this)'>"+cus.getDyUsername()+"</a></p>\n" + "" +
                          "     </div>\n" + "" +
                          "     <div class=\"pull-right p-r-5 p-t-5\">\n" + "" +
                          "         <p>"+cus.getDyCreattime().substring(10)+"</p>\n" + "" +
                          "     </div>\n" + "" +
                          "     <div class=\"pull-left col-md-12 col-sm-12 col-xs-12 m-b-3\" style=\"margin-top: -15px;padding-left: 50px;\">\n" + "" +
                          "         <small>跟进记录:"+cont+"</small>\n" + "" +
                          "     </div>\n" + "" +
                          "  </div>";
               }
               cus.setDyContent(content);
            }
         }
         return ResponseBo.ok(myusername,list);
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }

   @Override
   public List<CusDynamic> getExEmpReceData(CusDynamic cusDynamic) {
      try {
         /**时间格式化选择**/
         String starttime = cusDynamic.getStarttime();
         if("7".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-7);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else if ("30".equals(starttime)){
            Date month = DateUtil.addDays(new Date(),-30);
            String datestr = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else{
            /**时间格式化**/
            String endtime = cusDynamic.getEndtime();
            if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
               String timestart = starttime+" 00:00:00";
               String timeend = endtime+" 23:59:59";
               cusDynamic.setStarttime(timestart); cusDynamic.setEndtime(timeend);
            }
         }
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         String endtime = cusDynamic.getEndtime();
         String stattime = cusDynamic.getStarttime();
         if (!StringUtils.isAnyEmpty(stattime,endtime)){
            criteria.andBetween("dyCreattime",stattime,endtime+" 23:59:59");
         }

         /**人员查询**/
         List<String> selectUser = cusDynamic.getCreatorList();
         if (!selectUser.isEmpty()){
            criteria.andIn("dyCreator",selectUser);
         }
         List<String> listtype = new ArrayList<>();listtype.add("3");listtype.add("14");
         criteria.andEqualTo("dySourceType","1").andIn("dyType",listtype);
         /**排序**/
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> list = this.selectByExample(example);
         try {
            List<MyUser> user = userService.selectAll();
            for (CusDynamic cus:list){
               ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
               for (MyUser myUsers :user){
                  String userids = cus.getDyCreator();
                  if (userids.equals(myUsers.getUserUuid())){ cus.setDyCreatorName(myUsers.getNickname()); }
               }
               cus.setDyDisName(exEmp.getcExName());
               cus.setEndtime(exEmp.getcExTime());
               cus.setDyZhuanName(exEmp.getcExIntern());
            }
         }catch (Exception e){
            logger.error("获取客户失败："+e.getMessage());
         }
         return list;
      }catch (Exception e){
         logger.error("获取的推广资源领取数据失败："+e.getMessage());
      }
      return new ArrayList<>();
   }

   @Override
   public List<CusDynamic> getBigEmpReceData(CusDynamic cusDynamic) {
      try {
         /**时间格式化选择**/
         String starttime = cusDynamic.getStarttime();
         if("7".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-7);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else if ("30".equals(starttime)){
            Date month = DateUtil.addDays(new Date(),-30);
            String datestr = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else{
            /**时间格式化**/
            String endtime = cusDynamic.getEndtime();
            if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
               String timestart = starttime+" 00:00:00";
               String timeend = endtime+" 23:59:59";
               cusDynamic.setStarttime(timestart); cusDynamic.setEndtime(timeend);
            }
         }
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         String endtime = cusDynamic.getEndtime();
         String stattime = cusDynamic.getStarttime();
         if (!StringUtils.isAnyEmpty(stattime,endtime)){
            criteria.andBetween("dyCreattime",stattime,endtime+" 23:59:59");
         }
         /**人员查询**/
         List<String> selectUser = cusDynamic.getCreatorList();
         if (!selectUser.isEmpty()){
            criteria.andIn("dyCreator",selectUser);
         }
         List<String> listtype = new ArrayList<>();
         if (StringUtils.isEmpty(cusDynamic.getDySourceType())){
            //不限
            listtype.add("13");
            listtype.add("20");
            listtype.add("21");
         }else if (Constant.ZIYUAN_TYPE_AI.equals(cusDynamic.getDySourceType())){
            //AI资源
            listtype.add("20");
         }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(cusDynamic.getDySourceType())){
            //网校资源
            listtype.add("21");
         }else if (Constant.ZIYUAN_TYPE_BIGENP.equals(cusDynamic.getDySourceType())){
            listtype.add("13");
         }
         criteria.andIn("dyType",listtype);
         /**排序**/
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> list = this.selectByExample(example);
         try {
            List<MyUser> user = userService.selectAll();
            for (CusDynamic cus:list){
                if (Constant.ZIYUAN_TYPE_AI.equals(cus.getDySourceType())){
                  //AI资源
                   CAiEmp cAiEmp = caiEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                   cus.setDyDisName(cAiEmp.getAiName());
               }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(cus.getDySourceType())){
                  //网校资源
                   OnSchoolResource onSchoolResource = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                   cus.setDyDisName(onSchoolResource.getOsNickname());
               }else if (Constant.ZIYUAN_TYPE_BIGENP.equals(cus.getDySourceType())){
                   BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                   cus.setDyDisName(bigEmp.getBpName());
                }
               for (MyUser myUsers :user){
                  String userids = cus.getDyCreator();
                  if (userids.equals(myUsers.getUserUuid())){ cus.setDyCreatorName(myUsers.getNickname()); }
                  String useridsZhuan = cus.getDyDisUserid();
                  if (useridsZhuan.equals(myUsers.getUserUuid())){ cus.setDyZhuanName(myUsers.getNickname()); }
               }
            }
         }catch (Exception e){
            e.printStackTrace();
            logger.error("获取客户失败："+e.getMessage());
         }
         return list;
      }catch (Exception e){
         logger.error("获取的大资源分配数据失败："+e.getMessage());
      }
      return new ArrayList<>();
   }

   @Override
   public List<ExEmpRecevice> getExAllExportData(String ids,MyUser user) {
      List<ExEmpRecevice> listAll  = new ArrayList<>();
      try {
         List<String> list = Arrays.asList(ids.split(","));
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         /**推广资源条件**/
         List<String> listtype = new ArrayList<>();
         listtype.add("3");
         listtype.add("14");
         criteria.andEqualTo("dySourceType","1").andIn("dyType",listtype);
         criteria.andIn("dyCusUuid",list);
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> listALl = this.selectByExample(example);
         for (CusDynamic cus:listALl){
            ExEmpRecevice exEmpRecevice = new ExEmpRecevice();
            MyUser myUsers = userService.findUserDetailByUuids(cus.getDyCreator());

            if (myUsers !=null) {
               exEmpRecevice.setExRecevice(myUsers.getNickname());
               exEmpRecevice.setExDept(myUsers.getDeptName());
            }
            ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
            AutonCusInfo.parseExEmp(exEmp,user);
            MyUser myUsersDang = userService.findUserDetailByUuids(exEmp.getcExReceiver());

            if (myUsersDang !=null) {
               exEmpRecevice.setExReceviceCurent(myUsersDang.getNickname());
               exEmpRecevice.setExDeptCurent(myUsersDang.getDeptName());
            }

            exEmpRecevice.setExChat(exEmp.getcExWechat());
            exEmpRecevice.setExName(exEmp.getcExName());
            exEmpRecevice.setExPhone(exEmp.getcExMobile());
            exEmpRecevice.setExTime(cus.getDyCreattime());
            exEmpRecevice.setExctime(exEmp.getcExTime());
            exEmpRecevice.setExQq(exEmp.getcExQq());
            exEmpRecevice.setExChanel(exEmp.getcExChanel());
            exEmpRecevice.setExSource(exEmp.getcExSource());
            exEmpRecevice.setExType("推广资源");
            exEmpRecevice.setExFenType(cus.getDyType());
            exEmpRecevice.setExIntern(exEmp.getcExIntern());
            exEmpRecevice.setExStage(exEmp.getcExStage());
            exEmpRecevice.setExKey(exEmp.getcExKey());
            exEmpRecevice.setExUrl(exEmp.getcExInit());
            String creatname = "";
            MyUser user1 = userMapper.selectUserDetailByUuid(exEmp.getcExCreator());
            if(user1!=null){
               creatname = user1.getNickname();
            }else{
               creatname = exEmp.getcExCreator();
            }
            exEmpRecevice.setExCreator(creatname);

            //标签
            ExEmp ee = new ExEmp();
            ee.setcExId(Integer.parseInt(cus.getDyCusUuid()));
            ExEmp exEmpList = exEmpMapper.selectByPrimaryKey(ee);

            if(exEmpList!=null){
                String labelid = exEmpList.getcExLabel();
                StringBuffer sb  = new StringBuffer();
                if(!StringUtils.isEmpty(labelid)){
                    String[] labids = labelid.split(",");
                    for(String id : labids){
                        String tagname = saleTagDao.selectTagNameById(id);
                        sb.append(tagname+",");
                    }
                    String tags = sb.substring(0,sb.lastIndexOf(","));
                    exEmpRecevice.setExLabel(tags);
                }
            }

            Date cretime =  DateUtil.convert2Date(cus.getDyCreattime(),DateUtil.FULL_DATE_FORMAT);
            Date recetime = DateUtil.convert2Date(exEmp.getcExTime(),DateUtil.FULL_DATE_FORMAT);
            Long time = DateUtil.diffSecond(recetime,cretime);
            exEmpRecevice.setCrtime(time+" 秒");
            listAll.add(exEmpRecevice);
         }
      }catch (Exception e){
         e.printStackTrace();
         logger.error("动态资源导出领取人记录失败:"+e.getMessage());
      }
      return listAll;
   }

   @Override
   public List<CusDynamic> getCusTransData(CusDynamic cusDynamic) {
      try {
         /**时间格式化选择**/
         String starttime = cusDynamic.getStarttime();
         if("7".equals(starttime)){
            Date week = DateUtil.addDays(new Date(),-7);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else if ("30".equals(starttime)){
            Date month = DateUtil.addDays(new Date(),-30);
            String datestr = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
         }else{
            /**时间格式化**/
            String endtime = cusDynamic.getEndtime();
            if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
               String timestart = starttime+" 00:00:00";
               String timeend = endtime+" 23:59:59";
               cusDynamic.setStarttime(timestart); cusDynamic.setEndtime(timeend);
            }
         }
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         String endtime = cusDynamic.getEndtime();
         String stattime = cusDynamic.getStarttime();
         if (!StringUtils.isAnyEmpty(stattime,endtime)){
            criteria.andBetween("dyCreattime",stattime,endtime+" 23:59:59");
         }
         /**人员查询**/
         List<String> selectUser = cusDynamic.getCreatorList();
         if (!selectUser.isEmpty()){
            criteria.andIn("dyCreator",selectUser);
         }
         criteria.andEqualTo("dyType","5");
         /**排序**/
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> list = this.selectByExample(example);
         try {
            for (CusDynamic cus:list){
               MyUser myUsers = userService.findUserDetailByUuids(cus.getDyCreator());
               MyUser myUsers1 = userService.findUserDetailByUuids(cus.getDyZhuanUserid());
               if (myUsers !=null) { cus.setDyCreatorName(myUsers.getNickname()); }
               if (myUsers1!=null){cus.setDyZhuanName(myUsers1.getNickname());}
               String type = cus.getDySourceType();
               if (Constant.ZIYUAN_TYPE_EXEMP.equals(type)){
                  ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
                  cus.setDyDisName(exEmp.getcExName());
                  cus.setEndtime(exEmp.getcExTime());
               }else if (Constant.ZIYUAN_TYPE_BIGENP.equals(type)){
                  BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  cus.setDyDisName(bigEmp.getBpName());
                  cus.setEndtime(bigEmp.getBpCreatetime());
               }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(type)){
                  OnSchoolResource onSchoolResource = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  cus.setDyDisName(onSchoolResource.getOsNickname());
                  cus.setEndtime(onSchoolResource.getOsCreateTime());
               }else if (Constant.ZIYUAN_TYPE_MINE.equals(type)){
                  MyCustomer myCustomer = myCustomerMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                  cus.setDyDisName(myCustomer.getMyCusName());
                  cus.setEndtime(myCustomer.getMyCusCreatetime());
               }
            }
         }catch (Exception e){
            e.printStackTrace();
            logger.error("获取客户失败："+e.getMessage());
         }
         return list;
      }catch (Exception e){

         logger.error("获取的推广资源领取数据失败："+e.getMessage());
      }
      return new ArrayList<>();
   }

   @Override
   public List<ExEmpRecevice> getExAllTranExportData(String ids,MyUser myUser) {
      List<ExEmpRecevice> listAll  = new ArrayList<>();
      try {
         List<String> list = Arrays.asList(ids.split(","));
         Example example = new Example(CusDynamic.class);
         Example.Criteria criteria  =  example.createCriteria();
         criteria.andEqualTo("dyType","5");
         criteria.andIn("dyCusUuid",list);
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> listALl = this.selectByExample(example);
         for (CusDynamic cus:listALl){
            ExEmpRecevice exEmpRecevice = new ExEmpRecevice();
            MyUser myUsers = userService.findUserDetailByUuids(cus.getDyCreator());
            if (myUsers !=null) {
               exEmpRecevice.setExRecevice(myUsers.getNickname());
               exEmpRecevice.setExDept(myUsers.getDeptName());
            }
            exEmpRecevice.setExTime(cus.getDyCreattime());
            String type = cus.getDySourceType();
            if (Constant.ZIYUAN_TYPE_EXEMP.equals(type)){
               ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
               AutonCusInfo.parseExEmp(exEmp,myUser);
               exEmpRecevice.setExChat(exEmp.getcExWechat());
               exEmpRecevice.setExName(exEmp.getcExName());
               exEmpRecevice.setExType("推广资源");
               exEmpRecevice.setExPhone(exEmp.getcExMobile());
               exEmpRecevice.setExQq(exEmp.getcExQq());
               exEmpRecevice.setExChanel(exEmp.getcExChanel());
               exEmpRecevice.setExSource(exEmp.getcExSource());

            }else if (Constant.ZIYUAN_TYPE_BIGENP.equals(type)){
               BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
               AutonCusInfo.parseBigEmp(bigEmp,myUser);
               exEmpRecevice.setExName(bigEmp.getBpName());
               exEmpRecevice.setExPhone(bigEmp.getBpMobile());
               exEmpRecevice.setExQq(bigEmp.getBpQq());
               exEmpRecevice.setExType("大资源名单");
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(type)){
               OnSchoolResource onSchoolResource = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
               AutonCusInfo.parseOnSchoolResource(onSchoolResource,myUser);
               exEmpRecevice.setExName(onSchoolResource.getOsNickname());
               exEmpRecevice.setExPhone(onSchoolResource.getOsMobile());
               exEmpRecevice.setExQq(onSchoolResource.getOsQq());
               exEmpRecevice.setExType("网校资源");
            }else if (Constant.ZIYUAN_TYPE_MINE.equals(type)){
               MyCustomer myCustomer = myCustomerMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
               AutonCusInfo.parsePerson(myCustomer,myUser);
               exEmpRecevice.setExName(myCustomer.getMyCusName());
               exEmpRecevice.setExPhone(myCustomer.getMyCusMobile());
               exEmpRecevice.setExQq(myCustomer.getMyCusQq());
               exEmpRecevice.setExType("个人录入资源");
            }
            listAll.add(exEmpRecevice);
         }
      }catch (Exception e){
         logger.error("动态资源导出领取人记录失败:"+e.getMessage());
      }
      return listAll;
   }

   @Override
   public int deleteDynamic(List<String> ids) {
      try {
         Example example = new Example(CusDynamic.class);
         example.createCriteria().andIn("dyCusUuid",ids);
         return this.mapper.deleteByExample(example);
      }catch (Exception e){
         logger.error("删除推广资源后，删除响应得记录:"+e.getMessage());
      }
      return 0;
   }
   /**统计公海的当日领取**/
   @Override
   public int selectGongHaiResourceOverRevice(String userid) {
      try {
         Example example = new Example(CusDynamic.class);
         Example.Criteria criterion  = example.createCriteria();
         String date = DateUtil.getCurrentDate("yyyy-MM-dd");
         criterion.andEqualTo("dyType","15").andEqualTo("dyCreator",userid);
         criterion.andBetween("dyCreattime",date+" 00:00:00",date+" 23:59:59");
         return this.mapper.selectCountByExample(example);
      }catch (Exception e){
         logger.error("获取查询公海人数失败: "+e.getMessage());
      }
      return -1;
   }

   @Override
   public List<CusDynamic> selectCusDynamicByTimeAndUser(String startime, String endtime, String userid) {
      try {
         List<String> list = new ArrayList<>();
         list.add("13");list.add("3");list.add("7");list.add("15");list.add("5");
         Example example = new Example(CusDynamic.class);
         Example.Criteria criterion  = example.createCriteria();
         criterion.andBetween("dyCreattime",startime,endtime).andIn("dyType",list);
         Example.Criteria criterion1  = example.createCriteria();
         criterion1.orEqualTo("dyCreator",userid).orEqualTo("dyZhuanUserid",userid);
         criterion1.orEqualTo("dyDisUserid",userid);
         example.and(criterion1);
         return this.cusDynamicMapper.selectByExample(example);
      }catch (Exception e){
         logger.error("获取个人收到的信息失败:"+e.getMessage());
      }
      return new ArrayList<>();
   }

   @Override
   public List<CusDynamic> selectDialogByTimeAndUser(String startime, String endtime, String userid) {
      try {
         Example example = new Example(CusDynamic.class);
         Example.Criteria criterion  = example.createCriteria();
         criterion.andBetween("dyCreattime",startime,endtime).andEqualTo("dyType","11");
         Example.Criteria criterion1  = example.createCriteria();
         criterion1.orEqualTo("dyCreator",userid).orEqualTo("dyZhuanUserid",userid);
         criterion1.orEqualTo("dyDisUserid",userid);
         example.and(criterion1);
         return this.cusDynamicMapper.selectByExample(example);
      }catch (Exception e){
         logger.error("获取个人录音的信息失败:"+e.getMessage());
      }
      return new ArrayList<>();
   }

   @Override
   public CusDynamic selectExempChangeStatus(String extuuid) {
      try {
         Example example = new Example(CusDynamic.class);
         Example.Criteria criterion  = example.createCriteria();
         criterion.andEqualTo("dyCusUuid",extuuid).andEqualTo("dyType","16").andEqualTo("dySourceType","1");
         example.setOrderByClause("dy_creattime desc");
         List<CusDynamic> list =  this.cusDynamicMapper.selectByExample(example);
         if (!list.isEmpty()){
            return list.get(0);
         }
      }catch (Exception e){
         logger.error("查询推广资源阶段改变失败");
      }
      return null;
   }


   @Override
   public ResponseBo getFollowListByType(CusDynamic dynamic){
        try {
            CusDynamic cusDynamic = new CusDynamic();
            Date week = DateUtil.addDays(new Date(),-0);
            String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
            cusDynamic.setStarttime(datestr);
            cusDynamic.setEndtime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");

            Example example = new Example(CusDynamic.class);
            Example.Criteria criterion  = example.createCriteria();
            criterion.andBetween("dyCreattime",cusDynamic.getStarttime(),cusDynamic.getEndtime());

            Example.Criteria criterion1  = example.createCriteria();
            criterion1.andEqualTo("dyType","9").orEqualTo("dyType","22");

            if(dynamic.getCreatorList().size()>0){
               criterion.andIn("dyCreator",dynamic.getCreatorList());
            }

            example.and(criterion1);
            example.setOrderByClause(" dy_creattime desc");

            List<CusDynamic> list = this.selectByExample(example);
            if(list.size()>0){
               for (CusDynamic cus:list){
                  MyUser myUsers = userService.findUserByUuids(cus.getDyCreator());
                  if (myUsers !=null) cus.setDyCreatorName(myUsers.getNickname());
                  String type = cus.getDySourceType();
                  switch (type){
                     case Constant.ZIYUAN_TYPE_EXEMP:
                        ExEmp exEmp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(cus.getDyCusUuid()));
                        if(exEmp!=null){cus.setDyDisName(exEmp.getcExName());}

                        break;
                     case Constant.ZIYUAN_TYPE_BIGENP:
                        BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                        if(bigEmp!=null){
                           cus.setDyDisName(bigEmp.getBpName());
                        }
                        break;
                     case Constant.ZIYUAN_TYPE_SCHOLLE:
                        OnSchoolResource onSchoolRes = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                        if(onSchoolRes!=null){cus.setDyDisName(onSchoolRes.getOsNickname());}
                        break;
                     case Constant.ZIYUAN_TYPE_MINE:
                        MyCustomer myCustomer = myCustomerMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                        if(myCustomer!=null){cus.setDyDisName(myCustomer.getMyCusName());}
                        break;
                     case Constant.ZIYUAN_TYPE_AI:
                        CAiEmp aiEmp = caiEmpMapper.selectByPrimaryKey(Long.valueOf(cus.getDyCusUuid()));
                        if(aiEmp!=null){cus.setDyDisName(aiEmp.getAiName());}
                        break;
                     default:
                        cus.setDyDisName("-");
                  }

                  if("22".equals(cus.getDyType())){
                     String mid = cus.getDyMId()+"";
                     Example example1 = new Example(MRecord.class);
                     example1.createCriteria().andEqualTo("mId",mid);
                     List<MRecord> record = recordMapper.selectByExample(example1);
                     if(record.size()>0){
                        MRecord red = record.get(0);
                        cus.setDyRecord(red);
                     }
                  }
               }
            }
            return ResponseBo.ok("",list);
        }catch (Exception e){
            e.printStackTrace();
           return ResponseBo.error("获取失败");
        }
   }
}
